package com.gxa.email.service.impl;

import com.gxa.email.dto.MailDTO;
import com.gxa.email.exception.SystemException;
import com.gxa.email.gruop.Send;
import com.gxa.email.service.EmailService;
import com.gxa.email.validator.MyValidator;
import dto.ResultDTO;
import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.implementation.bytecode.Throw;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;

@Slf4j
@Service
public class EmailServiceImpl implements EmailService {

    @Autowired
    private JavaMailSender javaMailSender;

    @Resource
    private MyValidator validator;

    @Override
    public void sendEmail(MailDTO mailDTO){

        try{
            validator.validate(mailDTO, Send.class);
            //构建一个邮件对象
            SimpleMailMessage message = new SimpleMailMessage();
            //设置邮件主题
            message.setSubject(mailDTO.getSubject());
            //设置发送者
            message.setFrom(mailDTO.getFrom());
            //设置接收者
            message.setTo(mailDTO.getTo());
            //设置抄送者
            message.setCc(mailDTO.getCc());
            //设置发送时间
            message.setSentDate(new Date());
            //设置邮件的正文
            message.setText(mailDTO.getText());
            //发送邮件
            javaMailSender.send(message);

        }catch (SystemException e){
           throw new SystemException(1001,"请输入正确的邮箱号");
        }



    }

}
